package com.zjq.authoritysystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjq.authoritysystem.entity.User;
import com.zjq.authoritysystem.mapper.UserMapper;
import com.zjq.authoritysystem.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjq.authoritysystem.vo.query.UserQueryVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author BetOnMe
 * @since 2023-09-18
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public User getByUsername(String username) {
        return this.getOne(new QueryWrapper<User>().eq("username", username).eq("is_delete", User.IS_DELETE_NO));
    }

    @Override
    public Page findUsersByPage(UserQueryVo userQueryVo) {
        return this.page(new Page<>(userQueryVo.getCurrent(), userQueryVo.getSize()), new QueryWrapper<User>()
                .eq(ObjectUtils.isNotEmpty(userQueryVo.getDepartmentId()), "department_id", userQueryVo.getDepartmentId())
                .like(ObjectUtils.isNotEmpty(userQueryVo.getUsername()), "username", userQueryVo.getUsername())
                .like(ObjectUtils.isNotEmpty(userQueryVo.getRealName()), "real_name", userQueryVo.getRealName())
                .like(ObjectUtils.isNotEmpty(userQueryVo.getPhone()), "phone", userQueryVo.getPhone())
        );
    }

    @Override
    public IPage<User> findPages(UserQueryVo userQueryVo) {
        Page<User> page = new Page<>();
        page.setCurrent(userQueryVo.getCurrent());
        page.setSize(userQueryVo.getSize());
        return baseMapper.findUsersByPage(page, userQueryVo);
    }

    @Override
    public boolean deleteById(Integer id) {
        return this.update(new UpdateWrapper<User>()
                .set("is_delete", User.IS_DELETE_YES)
                .eq("id", id));
    }

}
